#include <stdio.h>
#include "../app.h"
#include "../font/font.h"
#include "dvc.h"

extern dvc_t gloab_dvc;
static uint8_t last_bat_num = 0;

static lv_obj_t *create_bat_info(lv_obj_t *parent)
{
    lv_obj_t *obj = lv_obj_create(parent);
    lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
    lv_obj_set_scroll_dir(obj, LV_DIR_NONE); /* 不允许滑动 - 没用触摸 - 禁用滑动条 */
    lv_obj_set_style_pad_all(obj, 0, LV_PART_MAIN);
    lv_obj_set_style_pad_hor(obj, 15, LV_PART_MAIN);
    lv_obj_set_style_border_width(obj, 0, LV_PART_MAIN);
    lv_obj_set_style_bg_color(obj, lv_color_make(165, 149, 254), LV_PART_MAIN);
    lv_obj_set_style_text_color(obj, lv_color_make(255, 255, 255), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(obj, LV_OPA_50, LV_PART_MAIN);

    lv_obj_t *id = lv_label_create(obj);
    lv_obj_align(id, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_text_font(id, base_font28, LV_PART_MAIN);
    lv_obj_set_style_text_color(id, lv_color_make(128, 128, 128), LV_PART_MAIN);

    lv_obj_t *label = lv_label_create(obj);
    lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
    return obj;
}

static void timer_task_cb(struct _lv_timer_t *timer)
{
    lv_obj_t *page = timer->user_data;
    if (gloab_dvc && gloab_dvc->bat_num != last_bat_num)
    {
        last_bat_num = lv_obj_get_child_cnt(page);
        for (uint8_t i = 2; i < last_bat_num; i++)
        {
            lv_obj_t *bat = lv_obj_get_child(page, i);
            lv_obj_add_flag(bat, LV_OBJ_FLAG_HIDDEN);
        }

        last_bat_num = gloab_dvc->bat_num;
        for (uint8_t i = 0; i < gloab_dvc->bat_num; i++)
        {
            lv_obj_t *bat = lv_obj_get_child(page, 2 + i);
            lv_obj_clear_flag(bat, LV_OBJ_FLAG_HIDDEN);
        }

        last_bat_num = gloab_dvc->bat_num;
    }

    if (last_bat_num)
    {
        uint32_t voltage_v = 0;
        for (uint8_t i = 0; i < last_bat_num; i++)
        {
            lv_obj_t *bat = lv_obj_get_child(page, 2 + i);
            lv_obj_t *label = lv_obj_get_child(bat, 1);
            voltage_v += gloab_dvc->bat_vol[i];
            lv_label_set_text_fmt(label, "%d.%03u", gloab_dvc->bat_vol[i] / 1000, gloab_dvc->bat_vol[i] % 1000);
        }
        {
            lv_obj_t *voltage = lv_obj_get_child(page, 0);
            lv_obj_t *label = lv_obj_get_child(voltage, 1);
            lv_label_set_text_fmt(label, "%lu.%03lu", voltage_v / 1000, voltage_v % 1000);
        }
        {
            lv_obj_t *diff_voltage = lv_obj_get_child(page, 1);
            lv_obj_t *label = lv_obj_get_child(diff_voltage, 1);
            lv_label_set_text_fmt(label, "%d.%03u", gloab_dvc->bat_diff / 1000, gloab_dvc->bat_diff % 1000);
        }
    }
}

lv_timer_t *app_bats_info_create(lv_obj_t *parent, uint8_t id)
{
    if (!parent)
        return NULL;

    lv_obj_t *obj = lv_tileview_add_tile(parent, id, 0, LV_DIR_HOR);
    lv_obj_set_style_pad_all(obj, 10, LV_PART_MAIN);
    lv_obj_set_style_text_font(obj, base_font18, LV_PART_MAIN);
    lv_obj_set_style_bg_color(obj, lv_color_make(0, 0, 0), LV_PART_MAIN);

    {
        lv_obj_t *voltage = create_bat_info(obj);
        lv_obj_t *id = lv_obj_get_child(voltage, 0);
        lv_obj_t *label = lv_obj_get_child(voltage, 1);
        lv_obj_set_style_pad_ver(voltage, 5, LV_PART_MAIN);
        lv_obj_set_style_pad_hor(voltage, 5, LV_PART_MAIN);
        lv_obj_set_size(voltage, LV_PCT(47), LV_SIZE_CONTENT);
        lv_obj_align(voltage, LV_ALIGN_TOP_LEFT, 5, 0);
        lv_obj_set_style_bg_color(voltage, lv_color_make(81, 170, 92), LV_PART_MAIN);
        lv_obj_set_style_text_font(id, base_font22, LV_PART_MAIN);
        lv_label_set_text_static(id, "电压");
        lv_obj_align(id, LV_ALIGN_LEFT_MID, -5, 0);
        lv_label_set_text_fmt(label, "0.000");
        lv_obj_align(label, LV_ALIGN_RIGHT_MID, 0, 0);
    }

    {
        lv_obj_t *diff_voltage = create_bat_info(obj);
        lv_obj_t *id = lv_obj_get_child(diff_voltage, 0);
        lv_obj_t *label = lv_obj_get_child(diff_voltage, 1);
        lv_obj_set_style_pad_ver(diff_voltage, 5, LV_PART_MAIN);
        lv_obj_set_style_pad_hor(diff_voltage, 5, LV_PART_MAIN);
        lv_obj_set_size(diff_voltage, LV_PCT(47), LV_SIZE_CONTENT);
        lv_obj_align(diff_voltage, LV_ALIGN_TOP_RIGHT, -5, 0);
        lv_obj_set_style_bg_color(diff_voltage, lv_color_make(0, 164, 231), LV_PART_MAIN);
        lv_obj_set_style_text_font(id, base_font22, LV_PART_MAIN);
        lv_label_set_text_static(id, "压差");
        lv_obj_align(id, LV_ALIGN_LEFT_MID, -5, 0);
        lv_label_set_text_fmt(label, "0.000");
        lv_obj_align(label, LV_ALIGN_RIGHT_MID, 0, 0);
    }

    lv_obj_t *last_bat = NULL, *last_last_bat = NULL, *lll_bat = NULL;
    for (uint8_t i = 0; i < 6; i++)
    {
        lv_obj_t *bat = create_bat_info(obj);
        lv_obj_t *id = lv_obj_get_child(bat, 0);
        lv_obj_t *label = lv_obj_get_child(bat, 1);
        lv_label_set_text_fmt(id, "%d", i);
        lv_label_set_text_fmt(label, "0.000");
        lv_obj_add_flag(bat, LV_OBJ_FLAG_HIDDEN);

        if (i < 3)
        {
            if (i == 0)
                lv_obj_align(bat, LV_ALIGN_LEFT_MID, 5, 0);
            else if (i == 1)
                lv_obj_align(bat, LV_ALIGN_CENTER, 0, 0);
            else
                lv_obj_align(bat, LV_ALIGN_RIGHT_MID, -5, 0);
        }
        else
            lv_obj_align_to(bat, lll_bat, LV_ALIGN_OUT_BOTTOM_MID, 0, 5);

        lll_bat = last_last_bat;
        last_last_bat = last_bat;
        last_bat = bat;
    }

    lv_timer_t *timer = lv_timer_create(timer_task_cb, 250, obj);
    lv_timer_pause(timer);
    return timer;
}

app_s bat_info_page = {
    .name = "bat_info",
    .create = app_bats_info_create,
    .delete = NULL,
    .update = NULL,
    .user_data = NULL,
};